package za.ac.lance.other;

import java.util.Scanner;

/**
 * File Name: GradeBookService.java
 * Version:
 * Description:
 * Package: za.ac.lance.encapsulation
 * Author: Lance
 * Date: Feb 25, 2012
 */
public class GradeBookServiceImpl implements GradeBookService
{

    @Override
    public void displayMessage(GradeBook gradeBook)
    {
        System.out.printf("Welcome to the grade book for\n%s!\n\n",
                gradeBook.getCourseName());
    }

    @Override
    public void inputGrades(GradeBook gradeBook)
    {
        Scanner input = new Scanner(System.in);

        int grade;

        System.out.printf("%s\n%s\n   %s\n   %s\n",
                "Enter the integer grades in the range 0-100.",
                "Type the end-of-file indicator to terminate input:",
                "On UNIX/Linux/Mac OS X type <Ctrl> d then press Enter",
                "On Windows type <Ctrl> z then press Enter");

        while (input.hasNext())
        {
            grade = input.nextInt();
            gradeBook.addToTotal(grade);
            gradeBook.incrementGradeCounter();

            incrementLetterGradeCounter(gradeBook, grade);
        }
    }

    @Override
    public void incrementLetterGradeCounter(GradeBook gradeBook, int grade)
    {
        switch (grade / 10)
        {
            case 9:
            case 10:
                gradeBook.incrementACount();
                break;

            case 8:
                gradeBook.incrementBCount();
                break;

            case 7:
                gradeBook.incrementCCount();
                break;

            case 6:
                gradeBook.incrementDCount();
                break;
            default:
                gradeBook.incrementFCount();
                break;
        }
    }

    @Override
    public void displayGradeReport(GradeBook gradeBook)
    {
        System.out.println("\nGrade Report:");

        if (gradeBook.getGradeCounter() != 0)
        {
            double average = (double) gradeBook.getTotal() / gradeBook.getGradeCounter();

            System.out.printf("Total of the %d grades entered is %d\n",
                    gradeBook.getGradeCounter(), gradeBook.getTotal());
            System.out.printf("Class average is %.2f\n", average);
            System.out.printf("%s\n%s%d\n%s%d\n%s%d\n%s%d\n%s%d\n",
                    "Number of students who received each grade:",
                    "A: ", gradeBook.getaCount(),
                    "B: ", gradeBook.getbCount(),
                    "C: ", gradeBook.getcCount(),
                    "D: ", gradeBook.getdCount(),
                    "F: ", gradeBook.getfCount());
        }
        else
        {
            System.out.println("No grades were entered");
        }
    }
}
